/**
 * elancom Corporation copy right 2012 - 2015  
 *   
 * $Header: https://192.168.1.224/svn/elancom/product/elancom-platform/trunk/src/main/java/com/elancom/framework/dao/expression/TypeUtils.java 1034 2012-07-02 03:09:12Z lanzhouxue $
 * $Id: TypeUtils.java 1034 2012-07-02 03:09:12Z lanzhouxue $
 * $Author: lanzhouxue $
 * $Date: 2012-07-02 11:09:12 +0800 (周一, 02 七月 2012) $
 * $Revision: 1034 $
 */
package com.elancom.framework.dao.expression;

import java.util.Collection;

import com.elancom.framework.commons.util.ForEach;
import com.elancom.framework.commons.util.JoyfulUtils;

/**
 * @author elancom
 * @date May 4, 2012
 */
public abstract class TypeUtils {
	public static Object toValue(Object value) {
		if (value instanceof String) {
			return "'" + value + "'";
		}

		return value;
	}

	public static Object toValue(Collection<Object> collection) {
		if (collection == null || collection.isEmpty()) {
			return "";
		}

		final StringBuilder sb = new StringBuilder();
		JoyfulUtils.forEach(collection, new ForEach<Object>() {
			@Override
			public boolean each(int index, Object value) {
				sb.append(TypeUtils.toValue(value));
				sb.append(",");
				return true;
			}
		});

		if (sb.length() != 0) {
			sb.deleteCharAt(sb.length() - 1);
		}

		return sb.toString();
	}

	public static Object toValue(Object[] objects) {
		if (objects == null || objects.length == 0) {
			return "";
		}

		final StringBuilder sb = new StringBuilder();
		JoyfulUtils.forEach(objects, new ForEach<Object>() {
			@Override
			public boolean each(int index, Object value) {
				sb.append(TypeUtils.toValue(value));
				sb.append(",");
				return true;
			}
		});

		if (sb.length() != 0) {
			sb.deleteCharAt(sb.length() - 1);
		}

		return sb.toString();
	}
}
